package com.aceitunaproject.server.time.tick;

import com.sun.sgs.app.AppContext;
import com.sun.sgs.app.ManagedReference;

/**
 * Tick manager interface implementation
 * 
 * @author Federico Bergstein
 */
public class TickManagerImpl implements TickManager {

	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * a reference to the ticker task
	 */
	private ManagedReference<TickerTask> tickerRef;
	
	//TODO: provide a method to restart the ticking with different duration
	//TODO get default values from configuration.
	
	/**
	 * delay time in minutes
	 */
	private int delay = 0;
	
	/**
	 * duration time in milliseconds
	 */
	private int duration = 20000;
	
	
	
	
	@Override
	public double getCurrentTick() {
		return tickerRef.get().getCurrentTick();
	}

	@Override
	public int getDelay() {
		
		return delay;
	}

	@Override
	public int getTickDuration() {
	
		return duration;
	}

	@Override
	public void setDelay(int delay) {
		this.delay = delay;

	}

	@Override
	public void setTickDuration(int duration) {
this.duration = duration;

	}

	@Override
	public void startTickTask() {
		TickerTask ticker = new TickerTask();
		tickerRef = AppContext.getDataManager().createReference(ticker);
		
		AppContext.getTaskManager().schedulePeriodicTask(ticker, delay, duration);
		

	}

	@Override
	public void startTickTask(int delay, int duration) {
		this.delay = delay;
		this.duration = duration;
		startTickTask();

	}

}
